<?php
/**
* 微信会员模型
*/
class Member_model extends MY_Model
{
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * [会员添加]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [修改内容]
	 * @return   [type]           [description]
	 */
	function memberAdd($post)
	{
		$res = $this->hw_Insert('member', $post);
		if ($res) {
			$where = array('id' => $res);
			$find = $this->hw_getRecord('member', $where, '*');

			$return = array(
				'code' => 1,
				'msg' => '会员添加成功',
				'data' => $find,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '会员添加失败',
			);
		}

		return $return;
	}

	/**
	 * [修改会员信息]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post      [修改内容]
	 * @return   [type]                [description]
	 */
	function memberUpdate($post)
	{
		$where = array('id' => $post['member_id']);
		$post['update_time'] = date('Y-m-d H:i:s');
		unset($post['member_id']);

		$res = $this->hw_Update('member', $post, $where);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '信息修改成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '信息修改失败',
			);
		}

		return $return;
	}

	/**
	 * [获取会员详情]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [会员标识]
	 * @return   [type]           [description]
	 */
	function memberDetail($post)
	{
		$where = array('open_id' => $post['open_id']);

		$find = $this->hw_getRecord('member', $where, '*');

		if ($find) {
			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $find,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '数据获取失败',
			);
		}

		return $return;
	}

	/**
	 * [作品发布]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [作品信息]
	 * @return   [type]    		  [description]
	 */
	function workAdd($post)
	{
		$data = array(
			'member_id' => $post['member_id'],
			'type' => $post['type'],
			'text' => $post['text'],
			'url' => $post['url'],
		);

		$res = $this->hw_Insert('member_work', $data);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '发布成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '发布失败',
			);
		}

		return $return;
	}

	/**
	 * [获取作品列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [会员数据]
	 * @return   [type]    		  [description]
	 */
	function workList($post)
	{
		$where = array('member_id' => $post['member_id'], 'del_status' => '0');

		$list = $this->hw_getList('member_work', $where, '*');

		if ($list) {
			foreach ($list as $key => &$value) {
				$value['url'] = explode(',', $value['url']);
			}

			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $list,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '无作品发布',
				'data' => array(),
			);
		}

		return $return;
	}

	/**
	 * [作品删除]
	 * @Author   zt
	 * @DateTime 2018-08-17
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function workDel($post)
	{
		$where = array('id' => $post['id'], 'member_id' => $post['member_id']);

		$data = array('del_status' => '1', 'update_time' => date('Y-m-d H:i:s'));

		$res = $this->hw_Update('member_work', $data, $where);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '删除成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '删除失败',
			);
		}

		return $return;
	}

	/**
	 * [档期添加]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [档期信息]
	 * @return   [type]           [description]
	 */
	function daysAdd($post)
	{
		$res = $this->hw_Insert('member_days', $post);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '添加成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '添加失败',
			);
		}

		return $return;
	}

	/**
	 * [获取档期列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function daysList($post)
	{
		$where = array(
			'member_id' => $post['member_id'],
			'del_status' => '0',
		);
		$like = array('date' => $post['date']);

		$list = $this->hw_getList_like('member_days', $where, '*', $like, 'both', 'date asc');

		if ($list) {
			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $list,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '暂无数据',
				'data' => array(),
			);
		}

		return $return;
	}

	/**
	 * [获取档期详情]
	 * @Author   zt
	 * @DateTime 2018-08-07
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function daysDetail($post)
	{
		$where = array('id' => $post['id']);

		$find = $this->hw_getRecord('member_days', $where, '*');

		if ($find) {
			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $find,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '暂无数据',
				'data' => array(),
			);
		}

		return $return;
	}

	/**
	 * [档期修改]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [档期信息]
	 * @return   [type]           [description]
	 */
	function daysUpdate($post)
	{
		$where = array('id' => $post['id']);
		$post['update_time'] = date('Y-m-d H:i:s');
		unset($post['id']);

		$res = $this->hw_Update('member_days', $post, $where);

		if ($res) {
			$return = array(
				'code' => 1,
				'msg' => '修改成功',
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '修改失败',
			);
		}

		return $return;
	}

	/**
	 * [获取知识星球列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $page [description]
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function loreList($page, $post)
	{
		$where = array('state' => '1','del_status' => '0');

		// type空为全部
		if (!empty($post['type'])) {
			$where['type'] = $post['type'];
		}

		$list = $this->hw_getList_limit('lore', $where, '*', $page);

		if ($list) {
			foreach ($list as $key => &$value) {
				// 获取图片、文件地址
				if ($value['txt_type'] == 0) {
					$img = $this->hw_getList_in('file_library', 'id', explode(',', $value['img']), '*');
					$value['img'] = $img;
				} elseif ($value['txt_type'] == 1) {
					$cover = $this->hw_getRecord('file_library', array('id' => $value['cover']), '*');
					$value['cover'] = $cover;
				} elseif ($value['txt_type'] == 2) {
					$file = $this->hw_getRecord('file_library', array('id' => $value['file']), '*');
					$value['file'] = array(
						'url' => base_url()."static/file/".md5($file['create_time']).'.'.$file['type'],
						'file_name' => $file['file_name'],
					);
				}
			}

			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $list,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '暂无数据',
				'data' => array(),
			);
		}

		return $return;
	}

	/**
	 * [获取查档列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function checkDays($post)
	{
		// 获取当前地区所有用户
		$where = array('area' => $post['area']);

		if (!empty($post['member_type'])) {
			$where['role'] = $post['member_type'];
		}

		$list = $this->hw_getList('member', $where, '*');

		if ($list) {
			foreach ($list as $key => $value) {
				// 判断用户当天是否有档期
				$where = array(
					'member_id' => $value['id'],
					'date' => $post['date'],
					'type' => $post['days_type'],
					'del_status' => '0'
				);
				$res = $this->hw_getRecord('member_days', $where, 'id');
				if ($res) {
					unset($list[$key]);
				}
			}

			sort($list);

			$return = array(
				'code' => 1,
				'msg' => '数据获取成功',
				'data' => $list,
			);
		} else {
			$return = array(
				'code' => 0,
				'msg' => '暂无数据',
				'data' => array(),
			);
		}

		return $return;
	}
}